// client.cpp
#define _WIN32_DCOM
#include <windows.h>
#include <iostream.h>
#include <stdio.h>
#include "component.h"

const CLSID CLSID_JavaComponent = { 0x2652CA66, 0xD6CF, 0x11D2, { 0xBB, 0x51, 0x00, 0x60, 0x97, 0xB5, 0xEA, 0xFC } };

void main()
{
	cout << "Client: Calling CoInitialize()" << endl;
	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	
	HRESULT hr;
	ISum* pSum;

	cout << "Client: Calling CoCreateInstance() " << endl;

	hr = CoCreateInstance(CLSID_JavaComponent, NULL, CLSCTX_INPROC_SERVER, IID_ISum, (void**)&pSum);

	if(FAILED(hr))
		cout << "FAILED" << endl;

	long sum;
	hr = pSum->Sum(5, 3, &sum);
	if(SUCCEEDED(hr))
		cout << "Sum = " << sum << endl;
	else
		cout << "Sum FAILED" << endl;

	hr = pSum->Sum(-2, 3, &sum);
	if(SUCCEEDED(hr))
		cout << "Sum = " << sum << endl;
	else
	{
		cout << "Sum FAILED" << endl;
		ISupportErrorInfo* pSupportErrorInfo;
		if(SUCCEEDED(pSum->QueryInterface(IID_ISupportErrorInfo, (void**)&pSupportErrorInfo)))
		{
			if(pSupportErrorInfo->InterfaceSupportsErrorInfo(IID_ISum) == S_OK)
			{
				IErrorInfo* pErrorInfo;
				GetErrorInfo(0, &pErrorInfo);

				BSTR description;
				pErrorInfo->GetDescription(&description);
				wprintf(L"HRESULT = %x, Description: %s\n", hr, description);
				SysFreeString(description);

				pErrorInfo->Release();
			}
			pSupportErrorInfo->Release();
		}
	}

	cout << "Client: Calling Release() for pSum" << endl;
	pSum->Release();

	cout << "Client: Calling CoUninitialize()" << endl;
	CoUninitialize();
}